package com.yoooya.base.admin.interceptor;

import com.yoooya.base.model.vo.LoginUser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

/**
 * 后台管理登入时候使用的session管理器
 * 
 * @创建人 Sun Yijing
 * @创建时间 2018-06-28
 */
public class SessionContext implements Serializable {
	private static final long serialVersionUID = 20091122L;
	public static final String KEY_IN_SESSION = "__SessionContext__Admin__";

	private Long userId;// 当前登陆管理员

	private Long agentId;

	private Long employeeId;

	private Long roleId;

	private Integer loginType;// 当前登陆管理员

	private LoginUser user;

	private boolean userLoginFlag = false;

	private Set<String> actionItems = new HashSet<String>();// 当前登录用户的权限集，在登录的时候加载

	private SessionContext() {

	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public Long getRoleId() {
		return roleId;
	}

	public void setRoleId(Long roleId) {
		this.roleId = roleId;
	}

	public LoginUser getUser() {
		return user;
	}

	public void setUser(LoginUser user) {
		this.user = user;
	}

	/**
	 * @return the userLoginFlag
	 */
	public boolean isUserLoginFlag() {
		return this.userLoginFlag;
	}

	/**
	 * @param userLoginFlag
	 *            the userLoginFlag to set
	 */
	public void setUserLoginFlag(boolean userLoginFlag) {
		this.userLoginFlag = userLoginFlag;
	}

	public void clear() {
		this.userLoginFlag = false;
		this.userId = null;
		this.user = null;
	}

	/**
	 * 返回当前session里面的sessionContext对象 此对象永远不会返回null，当前的session里面
	 * 没有此对象的时候，此方法会新建一个对象放到session里面
	 * 
	 * @param request
	 *            当前请求的对象
	 * @return SessionContext对象，永远不会为null
	 */
	public static SessionContext getSessionContext(HttpServletRequest request) {
		HttpSession session = request.getSession(true);
		return getSessionContext(session);
	}

	public static SessionContext getSessionContext(HttpSession session) {
		SessionContext sessionContext = (SessionContext) session
				.getAttribute(SessionContext.KEY_IN_SESSION);
		if (sessionContext == null) {
			sessionContext = new SessionContext();
			session.setAttribute(SessionContext.KEY_IN_SESSION, sessionContext);
		}
		return sessionContext;
	}

	public void initUserFunctionItems(Object currentUser) {

	}

	public Set<String> getActionItems() {
		return this.actionItems;
	}

	public void setActionItems(Set<String> actionItems) {
		this.actionItems = actionItems;
	}

	public Integer getLoginType() {
		return loginType;
	}

	public void setLoginType(Integer loginType) {
		this.loginType = loginType;
	}

	public Long getAgentId() {
		return agentId;
	}

	public void setAgentId(Long agentId) {
		this.agentId = agentId;
	}

	public Long getEmployeeId() {
		return employeeId;
	}

	public void setEmployeeId(Long employeeId) {
		this.employeeId = employeeId;
	}

}
